package com.uzai.console.service.main.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.main.notice.NoticeIdDto;
import com.uzai.console.dto.main.notice.NoticeQuery;
import com.uzai.console.entity.Notice;
import com.uzai.console.entity.NoticeRead;
import com.uzai.console.mapper.NoticeMapper;
import com.uzai.console.service.main.NoticeService;
import com.uzai.console.vo.main.notice.NoticeInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年08月25日 20:18
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    /***
     * 查询用户列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param noticeQuery
     * @return null
     */
    public Page<NoticeInfoVo> findNoticeList(NoticeQuery noticeQuery){
        //新建返回到页面的LIST对象
        List<NoticeInfoVo> noticeInfoVoList = new ArrayList<>();
        //执行分页查询对象
        Page<Notice> page = new Page<>();
        noticeQuery.convert(page);
        ArrayList<Notice> noticeList =  noticeMapper.findByList(page, noticeQuery);
        if(noticeList != null && noticeList.size() > 0){
            for (Notice notice : noticeList){
                NoticeInfoVo noticeInfoVo = new NoticeInfoVo();
                BeanUtils.copyProperties(notice, noticeInfoVo);
                //查询是否已读
                NoticeRead noticeRead = noticeMapper.selectNoticeRead(noticeQuery.getMerId(), notice.getId());
                if(noticeRead != null){
                    noticeInfoVo.setStatus(1); //已读
                }else{
                    noticeInfoVo.setStatus(0); //未读
                }
                noticeInfoVoList.add(noticeInfoVo);
            }
        }
        //返回到页面分页对象
        Page<NoticeInfoVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(noticeInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 根据公告信息
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param noticeIdDto
     * @return null
     */
    public NoticeInfoVo findNoticeInfo(NoticeIdDto noticeIdDto){

        NoticeInfoVo noticeInfoVo = new NoticeInfoVo();

        if(noticeIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        Notice notice = noticeMapper.selectById(noticeIdDto.getId());

        if(notice == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该公告不存在");
        }

        BeanUtils.copyProperties(notice, noticeInfoVo);

        return noticeInfoVo;
    }

    /**
     * 首次阅读添加已阅读记录
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param noticeId
     * @return null
     */
    public void firstReadNotice(Long merId, Long noticeId){
        //查询是否已读
        NoticeRead noticeRead = noticeMapper.selectNoticeRead(merId, noticeId);
        if(noticeRead == null){ //不存在，添加阅读记录
            Integer now = DateUtil.getNowTime();
            noticeRead = new NoticeRead();
            noticeRead.setId(IdWorker.getId());
            noticeRead.setMerId(merId);
            noticeRead.setNoticeId(noticeId);
            noticeRead.setCreateTime(now);
            noticeRead.setUpdateTime(now);
            noticeMapper.insertNoticeRead(noticeRead);
        }
    }

}
